package hongsong.eqixueweb.common;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;


public class BasicAction{
	
	public static  WebDriver driver ;
	public static WebDriverWait webwait;
	public static WebElement wAction;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {

//		System.setProperty("webdriver.chrome.driver", BasicAction.class.getResource("/driver/chromedriver.exe").getPath());
		System.setProperty("webdriver.gecko.driver", BasicAction.class.getResource("/driver/geckodriver.exe").getPath());
		driver = new FirefoxDriver();
		webwait= new WebDriverWait(driver, 30);
		driver.manage().window().maximize();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
//		driver.close();
		driver.quit();
	}

	 /**
     * 获取driver
     * */
    public WebDriver getDriver() {
        return driver;
    }
    /**
     * 自动截图
     * */
    public void takeScreenShot() {

        SimpleDateFormat sf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String dateStr = sf.format(date);
        String path =this.getClass().getSimpleName() + "_" + dateStr + ".png";
        System.out.println(path);
        //因为我们截图是需要用到driver的，所以这里需要获取driver，这个driver是获取的当前对象的driver
        takeScreenShot((TakesScreenshot) this.getDriver(), path);
       
    }
    /**
     * 传入参数截图
     * */
    public void takeScreenShot(TakesScreenshot drivername, String path) {
        File dir = new File("test-output/snapshot");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File scrFile = drivername.getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(scrFile, new File(dir.getAbsolutePath() + "\\" + path));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("截图成功");
        }
    }
	public static void sleep(int s){
		try {  
		    Thread.sleep(s*1000);  
		} catch (InterruptedException e) {  
		    e.printStackTrace();   
		} 
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}



}
